import { Effect } from 'dva';
import { Reducer } from 'redux';
import { queryCurrentSiteProfile } from '@/services/global';

interface GlobalModelState {
  siteProfile: any;
}

interface GlobalModelType {
  namespace: 'global';
  state: GlobalModelState;
  effects: {
    init: Effect;
  };
  reducers: {
    saveCurrentSiteProfile: Reducer<GlobalModelState>;
  };
}

const GlobalModel: GlobalModelType = {
  namespace: 'global',
  state: {
    siteProfile: {},
  },
  effects: {
    *init({ callback }, { call, put }) {
      const data = yield call(queryCurrentSiteProfile);
      yield put({
        type: 'saveCurrentSiteProfile',
        payload: { ...data }
      });
      if (callback) callback(data);
    }
  },
  reducers: {
    saveCurrentSiteProfile(state, action) {
      return {
        ...state,
        siteProfile: action.payload,
      };
    },
  }
};

export default GlobalModel;

